#!/usr/bin/env bash

# 初始化 fedora 设置
# 这样就不用每次安装虚拟机后还要手动配置或者没法好好用了
# 情景: 刚刚安装系统，什么都没有配置
# Todo:
# 1. 添加 rpm fusion 源
# 2. 添加 flathub 源
# 3. 安装基本软件
# curl https://gitee.com/corona09/fedora-init/raw/master/init.sh | sh

cwd=`pwd`

# {{{ 输出信息的辅助函数
success() {
	echo -e "\033[42;37m[SUCCESS]\033[0m $1"
}

msg() {
	if [ "$1" = "-n" ]; then
		echo -e "\n\033[47;30m[INFO]\033[0m $2"
	else
		echo -e "\033[47;30m[INFO]\033[0m $1"
	fi
}

err() {
	echo -e "\033[41;37m[ERROR]\033[0m $1"
}

# 检查上一条命令是否成功
# 成功则输出信息提示，失败则退出
success_or_not() {
	if test $? -ne 0; then
		err "发生错误"
		exit 1
	else
		success "$1"
	fi
}
# }}}

# 检测目录是否是 git 仓库的函数 {{{
is_git_repo() {
	if [ -d "$1" ] && cd "$1" && git rev-parse --is-inside-work-tree > /dev/null ; then
		echo 0
	else
		echo 1
	fi
}
# }}}

# 安装 npm 包 {{{
install_npm_package() {
	msg "安装 $1"
	npm list -g | grep -q "$1@" && success "$1 已安装" || {
		sudo npm install -g $1
		success_or_not "安装 $1 成功"
	}
}
# }}}

# 安装 pip 包 {{{
install_python_package() {
	pip list | grep -q "$1" && success "$1 已安装" || {
		sudo pip install $1
		success_or_not "安装 $1 成功"
	}
}
# }}}

# 1. 添加 rpmfusion 等软件源
msg -n "添加 rpmfusion 源"
ls /etc/yum.repos.d/ | grep -q "rpmfusion-nonfree.repo" && {
	success "rpmfusion 源已添加"
} || {
	sudo yum install -y --nogpgcheck https://mirrors.tuna.tsinghua.edu.cn/rpmfusion/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://mirrors.tuna.tsinghua.edu.cn/rpmfusion/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
	success_or_not "成功添加 rpmfusion 源"
}
# 添加 vscode 源
msg "添加 vscode 源"
ls /etc/yum.repos.d/ | grep -q "vscode"&& {
	success "vscode 源已添加"
} || {
	sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
	sudo sh -c 'echo -e "[code]\nname=visual studio code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
	success_or_not "成功添加 vscode 源"
}
# 添加 edge 源
msg "添加 microsoft-edge 源"
ls /etc/yum.repos.d/ | grep -q "edge" && {
	success "microsoft-edge 源已添加"
} || {
	sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
	sudo sh -c 'echo -e "[microsoft-edge]\nname=microsoft-edge\nbaseurl=https://packages.microsoft.com/yumrepos/edge/\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/microsoft-edge.repo'
	success_or_not "成功添加 microsoft-edge 源"
}

# 2. 添加 flathub 仓库
msg -n "添加 flathub 仓库"
flatpak remotes | grep -qE "^flathub(.*)filtered$"; a=$?
flatpak remotes | grep -qE "^flathub(.*)system$"  ; b=$?

if [ $a -eq 0 -o $b -eq 1 ]; then
	flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
	success_or_not "成功添加 flathub 仓库"
else
	success "已添加 flathub 仓库"
fi

sudo dnf --refresh upgrade -y

# 3. 更改 HOME 路径下各文件夹的名称
msg -n "更改各文件夹名称"
target=$HOME/.config/user-dirs.dirs
grep -n "桌面" "$target" && { sed -i 's/桌面/Dektop/'    "$target" ; }
grep -n "公共" "$target" && { sed -i 's/公共/Public/'    "$target" ; }
grep -n "视频" "$target" && { sed -i 's/视频/Videos/'    "$target" ; }
grep -n "音乐" "$target" && { sed -i 's/音乐/Music/'     "$target" ; }
grep -n "图片" "$target" && { sed -i 's/图片/Pictures/'  "$target" ; }
grep -n "文档" "$target" && { sed -i 's/文档/Documents/' "$target" ; }
grep -n "下载" "$target" && { sed -i 's/下载/Downloads/' "$target" ; }
grep -n "模板" "$target" && { sed -i 's/模板/Templates/' "$target" ; }

[[ -d "$HOME/桌面" ]] && { mv $HOME/桌面 $HOME/Desktop   ; }
[[ -d "$HOME/音乐" ]] && { mv $HOME/音乐 $HOME/Music     ; }
[[ -d "$HOME/下载" ]] && { mv $HOME/下载 $HOME/Downloads ; }
[[ -d "$HOME/文档" ]] && { mv $HOME/文档 $HOME/Documents ; }
[[ -d "$HOME/图片" ]] && { mv $HOME/图片 $HOME/Pictures  ; }
[[ -d "$HOME/视频" ]] && { mv $HOME/视频 $HOME/Videos    ; }
[[ -d "$HOME/公共" ]] && { mv $HOME/公共 $HOME/Public    ; }
[[ -d "$HOME/模板" ]] && { mv $HOME/模板 $HOME/Templates ; }
[[ -d "$HOME/桌面" ]] && { rm -rf        $HOME/桌面      ; }
[[ -d "$HOME/bin"  ]] && { rm -rf        $HOME/bin       ; }

success "成功更改文件夹名称"

# 4. 安装基本软件
msg "安装基本软件"

sudo dnf install -y gstreamer1-plugins-{bad-\*,good-\*,base} gstreamer1-plugin-openh264 gstreamer1-plugin-libav --exclude=gstreamer1-plugins-bad-free-devel
sudo dnf install -y \
	neovim python3-neovim python3-pip python3-numpy ranger fish exa ncdu bat git croc trash-cli fzf aria2 util-linux-user wget \
	calibre keepassxc telegram-desktop thunderbird thunderbird-wayland gnome-tweaks gnome-extensions-app gnome-browser-connector transmission-gtk papirus-icon-theme \
	unrar gzip p7zip p7zip-plugins \
	go glade gnome-builder devhelp clang nodejs nodejs-typescript \
	code google-chrome-stable microsoft-edge-stable \
	python3-devel libX11-devel libXext-devel fontconfig-devel harfbuzz-devel libXft-devel ncurses-devel
success_or_not "安装完成"

# 5. 拷贝设置文件
msg "拷贝设置文件"
if [ $(is_git_repo $HOME/Documents/dotfiles) -eq 0 ]; then
	# dotfiles 已存在
	cd $HOME/Documents/dotfiles && git pull
	success_or_not "dotfiles 已更新"
else
	rm -rf $HOME/Documents/dotfiles
	git clone --depth=1 https://gitee.com/corona09/dotfiles.git $HOME/Documents/dotfiles
	success_or_not "拷贝配置文件完成"
fi
for v in fish ranger zathura clash mpv pip go; do
	rm -rf $HOME/.config/$v && ln -sf $HOME/Documents/dotfiles/$v $HOME/.config/$v
done

rm -rf $HOME/.aria2                && ln -sf $HOME/Documents/dotfiles/aria2                  $HOME/.aria2
rm -rf $HOME/.bashrc               && ln -sf $HOME/Documents/dotfiles/bash/bashrc            $HOME/.bashrc
rm -rf $HOME/.npmrc                && ln -sf $HOME/Documents/dotfiles/npm/npmrc              $HOME/.npmrc
rm -rf $HOME/.config/starship.toml && ln -sf $HOME/Documents/dotfiles/starship/starship.toml $HOME/.config/starship.toml
rm -rf $HOME/.gitconfig            && ln -sf $HOME/Documents/dotfiles/git/config.ini         $HOME/.gitconfig
# ~/.cargo 目录不能删除, 因为这里可能有之前安装的 rust
mkdir -p $HOME/.cargo
rm -rf $HOME/.cargo/config
ln -sf $HOME/Documents/dotfiles/cargo/config $HOME/.cargo/config

user_home=$HOME
sudo mkdir -p /root/.config
for v in fish ranger starship.toml; do
	sudo rm -rf /root/.config/$v
	sudo ln -sf $user_home/.config/$v /root/.config/$v
done

msg "更改登录 shell"
sudo chsh -s /usr/bin/fish root
sudo chsh -s /usr/bin/fish corona

# 7. 安装部分源中没有的软件

# 安装 rust
msg "安装 rust"
if [ -d $HOME/.cargo/bin ] && [ -x $HOME/.cargo/bin/cargo ] && [ -x $HOME/.cargo/bin/rustup ] && [ -x $HOME/.cargo/bin/rustc ] ; then
	success "rust 已安装"
else
	msg "rust 未安装"
	curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
	success_or_not "安装 rust 成功"
fi

# 安装 starship
msg "安装 starship"
[[ -x /usr/local/bin/starship ]] || {
	curl -sS https://starship.rs/install.sh | sh
	success_or_not "安装 starship 成功"
} && {
	success "starship 已安装"
}

# scripts
msg "拷贝脚本文件"
if [ $(is_git_repo $HOME/Software/scripts) -eq 0 ]; then
	cd $HOME/Software/scripts && git pull
	success_or_not "更新脚本文件完毕"
else
	mkdir -p $HOME/Software
	rm -rf $HOME/Software/scripts
	git clone --depth=1 https://gitee.com/corona09/my-script $HOME/Software/scripts
	success_or_not "拷贝脚本文件完毕"
fi

# 百度网盘
msg "安装百度网盘 (cli)"
[[ -x /opt/local/BaiduPCS-Go/BaiduPCS-Go ]] || {
	cd $cwd
	mkdir -p files
	rm -f files/baidu.zip
	wget "https://download.fastgit.org/qjfoidnh/BaiduPCS-Go/releases/download/v3.8.7/BaiduPCS-Go-v3.8.7-linux-amd64.zip" -O files/baidu.zip
	sudo mkdir -p /opt/local
	sudo unzip files/baidu.zip -d /opt/local
	sudo mv /opt/local/BaiduPCS-Go-v3.8.7-linux-amd64 /opt/local/BaiduPCS-Go
	sudo rm -f /usr/local/bin/baidu-netdisk
	sudo ln -sf /opt/local/BaiduPCS-Go/BaiduPCS-Go /usr/local/bin/baidu-netdisk
	success_or_not "成功安装百度网盘 (cli)"
} && {
	success "百度网盘 (cli) 已安装"
}

# clangd
msg "安装 clangd"
[[ -x /opt/local/clangd/bin/clangd ]] || {
	cd $cwd
	mkdir -p files
	rm -f files/clangd.zip
	wget "https://download.fastgit.org/clangd/clangd/releases/download/15.0.0/clangd-linux-15.0.0.zip" -O files/clangd.zip
	sudo mkdir -p /opt/local
	sudo unzip files/clangd.zip -d /opt/local
	sudo mv /opt/local/clangd_15.0.0 /opt/local/clangd
	sudo rm -f /usr/local/bin/clangd
	sudo ln -sf /opt/local/clangd/bin/clangd /usr/local/bin/clangd
	success_or_not "成功安装 clangd"
} && {
	success "clangd 已经安装"
}

# clitrans
msg "安装 clitrans"
[[ -x /opt/local/clitrans/clitrans ]] || {
	cd $cwd
	mkdir -p files
	sudo mkdir -p /opt/local
	rm -f files/clitrans.tar.gz
	wget "https://download.fastgit.org/wfxr/clitrans/releases/download/v0.3.5/clitrans-v0.3.5-x86_64-unknown-linux-gnu.tar.gz" -O files/clitrans.tar.gz
	sudo tar -xvf files/clitrans.tar.gz --directory=/opt/local
	sudo mv /opt/local/clitrans-v0.3.5-x86_64-unknown-linux-gnu /opt/local/clitrans
	sudo rm -f /usr/local/bin/clitrans
	sudo ln -sf /opt/local/clitrans/clitrans /usr/local/bin/clitrans
	success_or_not "成功安装 clitrans"
} && {
	success "clitrans 已经安装"
}

# GBKunzip
msg "安装 GBKunzip"
[[ -x /opt/local/GBKunzip/gbkunzip ]] || {
	cd $cwd
	mkdir -p files
	sudo mkdir -p /opt/local/GBKunzip
	rm -f files/GBKunzip.zip
	wget "https://download.fastgit.org/Corona09/GBKunzip-by-lilydjwg/releases/download/0.0.1/GBKunzip.zip" -O files/GBKunzip.zip
	sudo unzip files/GBKunzip.zip -d /opt/local/GBKunzip/
	sudo chmod 755 /opt/local/GBKunzip/gbkunzip
	sudo rm -f /usr/local/bin/gbkunzip && sudo ln -sf /opt/local/GBKunzip/gbkunzip /usr/local/bin/gbkunzip
	success_or_not "成功安装 GBKunzip"
} && {
	success "GBKunzip 已经安装"
}

# lazygit
msg "安装 lazygit"
[[ -x /opt/local/lazygit/lazygit ]] || {
	cd $cwd
	mkdir -p files
	sudo mkdir -p /opt/local/lazygit
	rm -f files/lazygit.tar.gz
	wget "https://download.fastgit.org/jesseduffield/lazygit/releases/download/v0.35/lazygit_0.35_Linux_x86_64.tar.gz" -O files/lazygit.tar.gz
	sudo tar -xvf files/lazygit.tar.gz --directory=/opt/local/lazygit
	sudo rm -f /usr/local/bin/lazygit
	sudo ln -sf /opt/local/lazygit/lazygit /usr/local/bin/lazygit
	success_or_not "成功安装 lazygit"
} && {
	success "lazygit 已经安装"
}

# rust-analyzer
[[ -x /opt/local/rust-analyzer/rust-analyzer ]] || {
	cd $cwd
	mkdir -p files
	sudo mkdir -p /opt/local/rust-analyzer
	rm -f files/rust-analyzer.gz files/rust-analyzer
	wget "https://download.fastgit.org/rust-lang/rust-analyzer/releases/download/2022-09-19/rust-analyzer-x86_64-unknown-linux-gnu.gz" -O files/rust-analyzer.gz
	gzip -d files/rust-analyzer.gz
	sudo mv files/rust-analyzer /opt/local/rust-analyzer/rust-analyzer
	sudo chmod 755 /opt/local/rust-analyzer/rust-analyzer
	success_or_not "安装 rust-analyzer 成功"
} && {
	success "rust-analyzer 已安装"
}

# 安装 gopls
export GOPATH=$HOME/.go
[[ -x $GOPATH/bin/gopls ]] || {
	go install golang.org/x/tools/gopls@latest
	success_or_not "gopls 安装成功"
} && {
	success "gopls 已经安装"
}

# 安装 pip 包
install_python_package "ueberzug"

# 安装 npm 包
sudo npm config -g set registry https://registry.npm.taobao.org
install_npm_package "hexo"
install_npm_package "yarn"
install_npm_package "hexo-generator-index"

# 安装 st 终端
mkdir -p $HOME/Software

if [ $(is_git_repo $HOME/Software/st) -eq 0 ]; then
	cd $HOME/Software/st && git pull
	success_or_not "st 已存在"
else
	rm -rf $HOME/Software/st
	git clone --depth=1 https://gitee.com/corona09/st $HOME/Software/st
fi

cd $HOME/Software/st
sudo make install
cd $cwd

# 8. 安装字体

if [ $(ls /usr/local/share/fonts/FiraCode/ | grep -E '^Fira Code(.*).ttf$' | wc -l) -ge 15 ]; then
	success "FiraCode 字体已安装"
else
	mkdir -p files
	rm -rf files/FiraCode.zip
	wget "https://download.fastgit.org/ryanoasis/nerd-fonts/releases/download/v2.2.2/FiraCode.zip" -O files/FiraCode.zip
	sudo rm -rf /usr/local/share/fonts/FiraCode
	sudo mkdir -p /usr/local/share/fonts/FiraCode
	sudo unzip files/FiraCode.zip -d /usr/local/share/fonts/FiraCode
	success_or_not "FiraCode 字体安装成功"
fi

if [ $(ls /usr/local/share/fonts/Mononoki | grep -E '^mononoki(.*).ttf$' | wc -l) -ge 15 ]; then
	success "Mononoki 字体已安装"
else
	mkdir -p files
	rm -rf files/Mononoki.zip
	wget "https://download.fastgit.org/ryanoasis/nerd-fonts/releases/download/v2.2.2/Mononoki.zip" -O files/Mononoki.zip
	sudo rm -rf /usr/local/share/fonts/Mononoki
	sudo mkdir -p /usr/local/share/fonts/Mononoki
	sudo unzip files/Mononoki.zip -d /usr/local/share/fonts/Mononoki
	success_or_not "Mononoki 字体安装成功"
fi

# 9. 导入 dconf 配置
msg "导入 dconf 配置"
mkdir -p files
mkdir -p $HOME/.local/share/backgrounds/
wget "https://corona-oss.oss-cn-qingdao.aliyuncs.com/notes-img/2022-09-20_22-53-54.B9v8frT3Y.Cyberpunk-Edgerunner-Lucy-1.jpg" -O $HOME/.local/share/backgrounds/Lucy.jpg
# 头像 https://corona-oss.oss-cn-qingdao.aliyuncs.com/notes-img/2022-09-20_22-56-28.EZXzDNxFQ.Rosmontis.jpg
curl "https://gitee.com/corona09/fedora-init/raw/master/dconf.settings" > files/dconf.settings
cat files/dconf.settings | dconf load -f /
success_or_not "导入成功"

# 10. 安装各种 desktop 软件
# st clash
rm -rf $HOME/.local/share/applications/{clash,st}.desktop
mkdir -p $HOME/.local/share/applications
cp $HOME/Documents/dotfiles/desktop_entries/st.desktop $HOME/.local/share/applications/st.desktop
sudo cp $HOME/Documents/dotfiles/desktop_entries/icon/st.svg /usr/share/icons/hicolor/scalable/apps/st.svg
cp $HOME/Documents/dotfiles/desktop_entries/clash-startup.desktop $HOME/.local/share/applications/clash-startup.desktop
sudo cp $HOME/Documents/dotfiles/desktop_entries/icon/clash.png /usr/share/icons/hicolor/scalable/apps/clash.png

# 11. 安装 SunnyVim
# 安装本体
if [ $(is_git_repo $HOME/Software/SunnyVim) -eq 0 ]; then
	cd $HOME/Software/SunnyVim && git pull
	success "SunnyVim 已安装"
	cd $cwd
else
	rm -rf $HOME/Software/SunnyVim
	git clone --depth=1 https://gitee.com/corona09/SunnyVim.git $HOME/Software/SunnyVim
fi

rm -rf $HOME/.config/nvim && ln -sf $HOME/Software/SunnyVim $HOME/.config/nvim
sudo rm -rf /root/.config/nvim && sudo ln -sf $home/.config/nvim /root/.config/nvim
